package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import cn.hutool.core.date.DateUtil;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.DeptFileArchived;
import com.ruoyi.system.domain.MeetingCollect;
import com.ruoyi.system.domain.MeetingManagement;
import com.ruoyi.system.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.MeetingCollectUserMapper;
import com.ruoyi.system.domain.MeetingCollectUser;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;

/**
 * 会议收集人员Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-04
 */
@Service
public class MeetingCollectUserServiceImpl implements IMeetingCollectUserService
{
    @Autowired
    private MeetingCollectUserMapper meetingCollectUserMapper;
    @Autowired
    private IMeetingCollectService meetingCollectService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IDeptFileArchivedService deptFileArchivedService;

    /**
     * 查询会议收集人员
     *
     * @param id 会议收集人员主键
     * @return 会议收集人员
     */
    @Override
    public MeetingCollectUser selectMeetingCollectUserById(Long id)
    {
        return meetingCollectUserMapper.selectMeetingCollectUserById(id);
    }

    /**
     * 查询会议收集人员列表
     *
     * @param meetingCollectUser 会议收集人员
     * @return 会议收集人员
     */
    @Override
    public List<MeetingCollectUser> selectMeetingCollectUserList(MeetingCollectUser meetingCollectUser)
    {
        List<MeetingCollectUser> meetingCollectUsers = meetingCollectUserMapper.selectMeetingCollectUserList(meetingCollectUser);
        for(MeetingCollectUser vo : meetingCollectUsers){
            Long userId = vo.getUserId();
            SysUser user = sysUserService.selectUserById(userId);
            vo.setSysUser(user);
        }
        return meetingCollectUsers;
    }

    /**
     * 新增会议收集人员
     *
     * @param meetingCollectUser 会议收集人员
     * @return 结果
     */
    @Override
    public int insertMeetingCollectUser(MeetingCollectUser meetingCollectUser)
    {
        meetingCollectUser.setCreateTime(DateUtils.getNowDate());
        return meetingCollectUserMapper.insertMeetingCollectUser(meetingCollectUser);
    }

    /**
     * 修改会议收集人员
     *
     * @param meetingCollectUser 会议收集人员
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateMeetingCollectUser(MeetingCollectUser meetingCollectUser)
    {

        meetingCollectUser.setUpoladTime(DateUtils.getNowDate());
        meetingCollectUser.setUpdateTime(DateUtils.getNowDate());
        MeetingCollectUser meetingCollectUserVo = meetingCollectUserMapper.selectMeetingCollectUser(meetingCollectUser);
        Long collectId = meetingCollectUserVo.getCollectId();
        MeetingCollect meetingCollect = meetingCollectService.selectMeetingCollectById(collectId);
        Date endTime = meetingCollect.getEndTime();
        Date nowDate = DateUtils.getNowDate();
        int compare = DateUtil.compare(endTime, nowDate);
        if(compare>0){
            //提交
            meetingCollectUser.setStatus("1");
        }else{
            //补交
            meetingCollectUser.setStatus("2");
        }
        meetingCollectUser.setId(meetingCollectUserVo.getId());
        int i = meetingCollectUserMapper.updateMeetingCollectUser(meetingCollectUser);
        //添加对应归档文件
        Long meetingId = meetingCollect.getMeetingId();
        //获取会议归纳文件夹信息
        DeptFileArchived deptFileArchived = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(meetingId));
        //如果是上传之后重新编辑资料，删除归纳文件重新上传
        DeptFileArchived deptFileArchivedSummary = deptFileArchivedService.selectDeptFileArchivedByAssociationId(String.valueOf(meetingCollectUser.getId()));
        if(!ObjectUtils.isEmpty(deptFileArchivedSummary)){
            deptFileArchivedService.deleteDeptFileArchivedByAssociationId(deptFileArchivedSummary.getAssociationId());
        }
        //创建会后收集文件夹
        DeptFileArchived deptFileArchivedVo = new DeptFileArchived();
        deptFileArchivedVo.setDeptId(deptFileArchived.getDeptId());
        String createBy = meetingCollectUser.getCreateBy();
        deptFileArchivedVo.setCreateBy(createBy);
        SysUser user = sysUserService.selectUserById(Long.valueOf(createBy));
        String userName = user.getNickName();
        deptFileArchivedVo.setCreateName(userName);
        deptFileArchivedVo.setFileType("0");
        deptFileArchivedVo.setFileName("会后收集文件");
        deptFileArchivedVo.setParentId(deptFileArchived.getParentId());
        deptFileArchivedVo.setAssociationId(String.valueOf(meetingCollectUser.getId()));
        Long archivedId = deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo);
        //创建会后收集文件
        DeptFileArchived deptFileArchivedVo2 = new DeptFileArchived();
        deptFileArchivedVo2.setDeptId(deptFileArchived.getDeptId());
        deptFileArchivedVo2.setCreateBy(createBy);
        deptFileArchivedVo2.setCreateName(userName);
        deptFileArchivedVo2.setFileType("1");
        deptFileArchivedVo2.setFileName("会后收集文件附件");
        deptFileArchivedVo2.setFileUrl(meetingCollectUser.getFileUrl());
        deptFileArchivedVo2.setAssociationId(String.valueOf(meetingCollectUser.getId()));
        deptFileArchivedVo2.setParentId(archivedId);
        deptFileArchivedService.insertDeptFileArchived(deptFileArchivedVo2);
        return i;
    }
    /**
     * 删除会议收集人员信息
     *
     * @param id 会议收集人员主键
     * @return 结果
     */
    @Override
    public int deleteMeetingCollectUserById(Long id)
    {
        return meetingCollectUserMapper.deleteMeetingCollectUserById(id);
    }

}
